/*
	后台-系统设置控制器
*/
'use strict';
var moment		= require('moment');
var AV 				= require('leanengine');
var mdCommon	= require('../modules/common');

var avSysConfig 	= AV.Object.extend("SysConfig");
var avImage 			= AV.Object.extend("Image");
var avTrip 				= AV.Object.extend("Trip");
var avNews 				= AV.Object.extend("New");

/**
 * 列表页面
 * 2016.04.22  by 胡斌
 */
exports.newBannerPage = function(req, res) {
	//console.dir(req.AV.user);
	var name	= req.query.name;

	var querySysConfig 	= new AV.Query(avSysConfig);
	querySysConfig.equalTo("name","new_banner");
	querySysConfig.first().then(function(result){
		if(typeof(result)=="undefined"){
			var newSysConfig=new avSysConfig();
			newSysConfig.set("name","new_banner");
			newSysConfig.set("value",new Array(0));
			return newSysConfig.save();
		}else{
			return AV.Promise.as(result);
		}
	}).then(function(result) {
		var val=result.get("value");
		res.render('admin-setting-newbanner',{
			user:req.AV.user,
			results:eval("("+val+")"),
			moment:moment,
			name:name
		});
	},function(error) {
		mdCommon.sentErr(res,req,error);
	});
}

/**
 * 列表页面
 * 2016.04.22  by 胡斌
 */
exports.homeCatagoryPage = function(req, res) {
	//console.dir(req.AV.user);
	var name	= req.query.name;

	var querySysConfig 	= new AV.Query(avSysConfig);
	querySysConfig.equalTo("name","home_catagory");
	querySysConfig.first().then(function(result){
		if(typeof(result)=="undefined"){
			var newSysConfig=new avSysConfig();
			newSysConfig.set("name","home_catagory");
			newSysConfig.set("value",new Array(0));
			return newSysConfig.save();
		}else{
			return AV.Promise.as(result);
		}
	}).then(function(result) {
		var val=result.get("value");
		res.render('admin-setting-homecatagory',{
			user:req.AV.user,
			results:eval("("+val+")"),
			moment:moment,
			name:name
		});
	},function(error) {
		mdCommon.sentErr(res,req,error);
	});
}



/**
 * 文章管理-保存
 * 2015.05.24  by 胡斌
 */
exports.doSaveBanner = function(req, res) {
	//console.log("doSave");
	var tagArr	= req.body.tagArr;
	console.log(tagArr);

	var querySysConfig 	= new AV.Query(avSysConfig);
	querySysConfig.equalTo("name","new_banner");
	querySysConfig.first().then(function(result){
		if(typeof(result)=="undefined"){
			var newSysConfig=new avSysConfig();
			newSysConfig.set("name","new_banner");
			newSysConfig.set("value",tagArr);
			return newSysConfig.save();
		}else{
			newSysConfig=result;
			newSysConfig.set("value",tagArr);
			return newSysConfig.save();
		}
	}).then(function(result) {
		//console.dir(result);
		if(req.body.ajax){
			res.json({result:result});
		}else{
			res.redirect('admin/config/newbanner');
		}
	},function(error){
		console.dir(error);
		mdCommon.sentErr(res,req,error);
	});
}

/**
 * 文章管理-保存
 * 2015.05.24  by 胡斌
 */
exports.doSaveCatagory = function(req, res) {
	//console.log("doSave");
	var catagoryArr	= req.body.catagoryArr;
	console.log(catagoryArr);

	var querySysConfig 	= new AV.Query(avSysConfig);
	querySysConfig.equalTo("name","home_catagory");
	querySysConfig.first().then(function(result){
		if(typeof(result)=="undefined"){
			var newSysConfig=new avSysConfig();
			newSysConfig.set("name","home_catagory");
			newSysConfig.set("value",catagoryArr);
			return newSysConfig.save();
		}else{
			newSysConfig=result;
			newSysConfig.set("value",catagoryArr);
			return newSysConfig.save();
		}
	}).then(function(result) {
		//console.dir(result);
		if(req.body.ajax){
			res.json({result:result});
		}else{
			res.redirect('admin/config/homecatagory');
		}
	},function(error){
		console.dir(error);
		mdCommon.sentErr(res,req,error);
	});
}

/**
 * 文章管理-保存
 * 2015.05.24  by 胡斌
 */
exports.doGetItem = function(req, res) {
	var query 			= new AV.Query(avImage);
	var id					= req.query.id;
	var type				= req.query.type;
	if(type=='news')query = new AV.Query(avNews);
	if(type=='trip')query = new AV.Query(avTrip);

	query.get(id).then(function(result) {
		var item={};
		if(type=='news' || type=='trip'){
			if(result.get("cover"))item.image=result.get("cover").id;
			item.title=result.get("title");
			item.id		=result.id;
		}else{
			item.image=result.id;
			item.title=result.get("name");
			item.id		=result.id;
		}

		res.json({result:item});
	},function(error) {
		res.json({error:error.message});
	});
}
